package com.wanghai.carwashing.common.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

/**
 * JSON工具类
 * @author 王海
 */
public class JsonHelper {
    
    public final static String EMPTY = "";
    
    public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    private JsonHelper(){
        
    }
    /**
     * 构建具备content-type为application/json的headers
     * @return 
     */
    public static  HttpHeaders buildJsonHeaders(){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }
    /**
     * 把对象转换成json字符串
     * @param obj 待转换的对象
     * @return 
     */
    public static String buildJsonString(Object obj){
        String jsonString = JSON.toJSONString(obj,SerializerFeature.DisableCircularReferenceDetect);
	return jsonString;
    }
    /**
     * 把一个json字符串解析成一个对象
     * @param <T> 对象的
     * @param jsonString
     * @param cls 对象的类型
     * @return
     */
    public final static <T> T parseObject(String jsonString,Class<T> cls){
        T t = JSON.parseObject(jsonString,cls);
        return t;
    }
    
    /**
    * 把一个json字符串解析成一个对象数组
    * @param <T>
    * @param jsonString
    * @param cls
    * @return
    */
    public final static <T> List<T> parseArray(String jsonString,Class<T> cls){
        List<T> list = new  ArrayList<T>();
        list = JSON.parseArray(jsonString,cls);
        return list;
    }
    
    /**
    * 把一个json字符串转换成一个map的集合
    * @param jsonString
    * @return
    */
    public final static List<Map<String, Object>> parseMapList(String jsonString){
        List<Map<String,Object>> list = JSON.parseObject(jsonString,
                        new TypeReference<List<Map<String,Object>>>(){});
        return list;
    }
   /**
    * 把一个json字符串转换成一个map
    * @param jsonString
    * @return
    */
    public final static Map<String, Object> parseMap(String jsonString){
        Map<String,Object> map = JSON.parseObject(jsonString,
                        new TypeReference<Map<String,Object>>(){});
        return map;
    }
}
